package com.team.mail.seckill.interceptor;


import com.team.common.constant.AuthServerConstant;
import com.team.common.vo.MemberResVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 秒杀服务
 * <p>
 * 拦截器·
 * 想起作用还得加一个配置 OrderWebConfiguration
 */

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResVo> loinUser = new ThreadLocal<> ();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //放行这个/order/order/status/** 请求路径、匹配到就放行
        //重要给WareSkuServiceImpl 这个实现类里面的远程调用使用
        String uri = request.getRequestURI ();
        AntPathMatcher antPathMatcher = new AntPathMatcher ();
        boolean match = antPathMatcher.match ("/kill", uri);

        if (match) {
            MemberResVo attribute = (MemberResVo) request.getSession ().getAttribute (AuthServerConstant.LOGIN_USER);
            if (attribute != null) {
                loinUser.set (attribute);
                return true;
            } else {
                //没有就去登录
                request.getSession ().setAttribute ("msg", "请先登录");
                response.sendRedirect ("http://auth.gulimall.com/login.html");
                return false;
            }
        }
        return true;
    }
}
